package com.moqianfan.mcuassistant.activity;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.moqianfan.mcuassistant.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/8.
 * 数码管代码查询
 */

public class LedCodeActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.btn_a)
    Button aBtn;
    @BindView(R.id.btn_b)
    Button bBtn;
    @BindView(R.id.btn_c)
    Button cBtn;
    @BindView(R.id.btn_d)
    Button dBtn;
    @BindView(R.id.btn_e)
    Button eBtn;
    @BindView(R.id.btn_f)
    Button fBtn;
    @BindView(R.id.btn_g)
    Button gBtn;
    @BindView(R.id.btn_dp)
    Button dpBtn;
    @BindView(R.id.BtnAcode)
    Button AcodeBtn;
    @BindView(R.id.BtnCcode)
    Button CcodeBtn;
    @BindView(R.id.ResaultTv)
    TextView tvResult;
    @BindView(R.id.Code)
    TextView Edt;
    @BindView(R.id.ImageView)
    ImageView img;
    @BindView(R.id.radioGroup1)
    RadioGroup group;

    Boolean flag = false;
    Boolean a = false, b = false, c = false, d = false, e = false, f = false,
            g = false, dp = false;
    byte[] arry = new byte[8];
    long editclickTime = 0;
    ClipboardManager myClipboard;

    @Override
    public void findViews() {
        setContentView(R.layout.activity_led);
    }

    @Override
    public void initViews() {
        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        aBtn.setOnClickListener(this);
        bBtn.setOnClickListener(this);
        cBtn.setOnClickListener(this);
        dBtn.setOnClickListener(this);
        eBtn.setOnClickListener(this);
        fBtn.setOnClickListener(this);
        gBtn.setOnClickListener(this);
        dpBtn.setOnClickListener(this);
        AcodeBtn.setOnClickListener(this);
        CcodeBtn.setOnClickListener(this);
        group.setOnCheckedChangeListener(listener);
        Edt.setOnClickListener(this);
        Edt.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public void initDatas() {

    }

    /**
     * 判断数码管共阴或共阳
     */
    private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.ying) {
                img.setBackgroundResource(R.drawable.ying);
                flag = false;
                String str = bytesToHexString(arry);
                if (str.length() == 1)
                    tvResult.setText(String.format("0%s", str));
                else
                    tvResult.setText(str);
            }
            if (checkedId == R.id.yang) {
                flag = true;
                img.setBackgroundResource(R.drawable.yang);
                String str = bytesToHexString2(arry);
                if (str.length() == 1)
                    tvResult.setText(String.format("0%s", str));
                else
                    tvResult.setText(str);
            }
        }
    };

    @OnClick({R.id.image_close, R.id.btn_a, R.id.btn_b, R.id.btn_c, R.id.btn_d, R.id.btn_e, R.id.btn_f,
            R.id.btn_g,R.id.btn_dp, R.id.BtnAcode, R.id.BtnCcode, R.id.Code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                overridePendingTransition(R.anim.push_right_in,
                        R.anim.push_right_out);
                break;

            case R.id.btn_a:
                if (!a) {
                    aBtn.setBackgroundResource(R.drawable.a_p);
                    a = true;
                    arry[0] = 1;
                } else {
                    aBtn.setBackgroundResource(R.drawable.a_n);
                    a = false;
                    arry[0] = 0;
                }
                break;
            case R.id.btn_b:
                if (!b) {
                    bBtn.setBackgroundResource(R.drawable.b_p);
                    b = true;
                    arry[1] = 1;
                } else {
                    bBtn.setBackgroundResource(R.drawable.b_n);
                    b = false;
                    arry[1] = 0;
                }
                break;
            case R.id.btn_c:
                if (!c) {
                    cBtn.setBackgroundResource(R.drawable.c_p);
                    c = true;
                    arry[2] = 1;
                } else {
                    cBtn.setBackgroundResource(R.drawable.c_n);
                    c = false;
                    arry[2] = 0;
                }
                break;
            case R.id.btn_d:
                if (!d) {
                    dBtn.setBackgroundResource(R.drawable.d_p);
                    d = true;
                    arry[3] = 1;
                } else {
                    dBtn.setBackgroundResource(R.drawable.d_n);
                    d = false;
                    arry[3] = 0;
                }
                break;
            case R.id.btn_e:
                if (!e) {
                    eBtn.setBackgroundResource(R.drawable.e_p);
                    e = true;
                    arry[4] = 1;
                } else {
                    eBtn.setBackgroundResource(R.drawable.e_n);
                    e = false;
                    arry[4] = 0;
                }
                break;
            case R.id.btn_f:
                if (!f) {
                    fBtn.setBackgroundResource(R.drawable.f_p);
                    f = true;
                    arry[5] = 1;
                } else {
                    fBtn.setBackgroundResource(R.drawable.f_n);
                    f = false;
                    arry[5] = 0;
                }
                break;
            case R.id.btn_g:
                if (!g) {
                    gBtn.setBackgroundResource(R.drawable.g_p);
                    g = true;
                    arry[6] = 1;
                } else {
                    gBtn.setBackgroundResource(R.drawable.g_n);
                    g = false;
                    arry[6] = 0;
                }
                break;
            case R.id.btn_dp:
                if (!dp) {
                    dpBtn.setBackgroundResource(R.drawable.dp_p);
                    dp = true;
                    arry[7] = 1;
                } else {
                    dpBtn.setBackgroundResource(R.drawable.dp_n);
                    dp = false;
                    arry[7] = 0;
                }
                break;
            case R.id.BtnAcode:
                if (!flag) {
                    String str = "DB  3FH //'0'";
                    str = str + "\nDB  06H  //'1'";
                    str = str + "\nDB  5BH  //'2'";
                    str = str + "\nDB  4FH  //'3'";
                    str = str + "\nDB  66H  //'4'";
                    str = str + "\nDB  6DH  //'5'";
                    str = str + "\nDB  7DH  //'6'";
                    str = str + "\nDB  07H  //'7'";
                    str = str + "\nDB  7FH  //'8'";
                    str = str + "\nDB  6FH  //'9'";
                    str = str + "\nDB  77H  //'A'";
                    str = str + "\nDB  7CH  //'B'";
                    str = str + "\nDB  39H  //'C'";
                    str = str + "\nDB  5EH  //'D'";
                    str = str + "\nDB  79H  //'E'";
                    str = str + "\nDB  71H   //'F'";
                    Edt.setText(str);
                } else {
                    String str = "";
                    str = str + "DB  0C0H  //'0'";
                    str = str + "\nDB  0F9H  //'1'";
                    str = str + "\nDB  0A4H  //'2'";
                    str = str + "\nDB  0B0H  //'3'";
                    str = str + "\nDB  99H  //'4'";
                    str = str + "\nDB  92H  //'5'";
                    str = str + "\nDB  82H  //'6'";
                    str = str + "\nDB  0F8H  //'7'";
                    str = str + "\nDB  80H  //'8'";
                    str = str + "\nDB  90H  //'9'";
                    str = str + "\nDB  88H  //'A'";
                    str = str + "\nDB  83H  //'B'";
                    str = str + "\nDB  0C6H  //'C'";
                    str = str + "\nDB  0A1H  //'D'";
                    str = str + "\nDB  86H  //'E'";
                    str = str + "\nDB  8EH  //'F'";
                    Edt.setText(str);
                }
                break;

            case R.id.BtnCcode:
                if (!flag) {
                    String str = "unsigned char leddata[]={";
                    str = str + "\n0x3F,  //'0'";
                    str = str + "\n0x06,  //'1'";
                    str = str + "\n0x5B,  //'2'";
                    str = str + "\n0x4F,  //'3'";
                    str = str + "\n0x66,  //'4'";
                    str = str + "\n0x6D,  //'5'";
                    str = str + "\n0x7D,  //'6'";
                    str = str + "\n0x07,  //'7'";
                    str = str + "\n0x7F,  //'8'";
                    str = str + "\n0x6F,  //'9'";
                    str = str + "\n0x77,  //'A'";
                    str = str + "\n0x7C,  //'B'";
                    str = str + "\n0x39,  //'C'";
                    str = str + "\n0x5E,  //'D'";
                    str = str + "\n0x79,  //'E'";
                    str = str + "\n0x71  //'F'";
                    str = str + "\n};";
                    Edt.setText(str);
                } else {
                    String str = "unsigned char leddata[]={";
                    str = str + "\n0xC0,  //'0'";
                    str = str + "\n0xF9,  //'1'";
                    str = str + "\n0xA4,  //'2'";
                    str = str + "\n0xB0,  //'3'";
                    str = str + "\n0x99,  //'4'";
                    str = str + "\n0x92,  //'5'";
                    str = str + "\n0x82,  //'6'";
                    str = str + "\n0xF8,  //'7'";
                    str = str + "\n0x80,  //'8'";
                    str = str + "\n0x90,  //'9'";
                    str = str + "\n0x88,  //'A'";
                    str = str + "\n0x83,  //'B'";
                    str = str + "\n0xC6,  //'C'";
                    str = str + "\n0xA1,  //'D'";
                    str = str + "\n0x86,  //'E'";
                    str = str + "\n0x8E  //'F'";
                    str = str + "\n};";
                    Edt.setText(str);
                }
                break;

            case R.id.Code:
                if (System.currentTimeMillis() - editclickTime > 500) {
                    editclickTime = System.currentTimeMillis();
                } else {
                    ClipData myClip;
                    String text = Edt.getText().toString();//如果有内容直接添加就好
                    myClip = ClipData.newPlainText("text", text);//text是内容
                    myClipboard.setPrimaryClip(myClip);
                    Toast.makeText(this, "内容已复制", Toast.LENGTH_SHORT).show();
                }
                break;

        }
        if (!flag) {
            String str = bytesToHexString(arry);
            if (str.length() == 1)
                tvResult.setText(String.format("0%s", str));
            else
                tvResult.setText(str);
        } else {
            String str = bytesToHexString2(arry);
            if (str.length() == 1)
                tvResult.setText(String.format("0%s", str));
            else
                tvResult.setText(str);
        }
    }

    /**
     * 共阴数码管数组转换为string型
     */
    public static String bytesToHexString(byte[] bArray) {
        StringBuilder sb = new StringBuilder(bArray.length);
        String sTemp;
        for (int i = bArray.length - 1; i > -1; i--) {
            sTemp = Integer.toBinaryString(0x1 & bArray[i]);
            sb.append(sTemp);
        }
        return Long.toHexString(Long.parseLong(sb.toString(), 2)).toUpperCase();
    }

    /**
     * 共阳数码管数组转换为string型
     */
    @SuppressLint("DefaultLocale")
    public static String bytesToHexString2(byte[] bArray) {
        StringBuilder sb = new StringBuilder(bArray.length);
        String sTemp;
        for (int i = bArray.length - 1; i > -1; i--) {
            sTemp = Integer.toBinaryString(0x1 & ~bArray[i]);
            sb.append(sTemp);
        }
        return Long.toHexString(Long.parseLong(sb.toString(), 2)).toUpperCase();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_right_out);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}
